<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="美食分类" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入美食分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="美食标题" prop="tile">
        <el-input
          v-model="queryParams.tile"
          placeholder="请输入美食标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['guide:cookbook:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['guide:cookbook:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['guide:cookbook:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['guide:cookbook:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cookbookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="美食分类" align="center" prop="className" />
      <el-table-column label="美食标题" align="center" prop="tile" />
      <el-table-column label="地址" align="center" prop="url" >
        <template slot-scope="scope">
          <el-link :href="scope.row.url" target="_blank">{{scope.row.url}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="封面图" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
              v-hasPermi="['guide:cookbook:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['guide:cookbook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['guide:cookbook:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改美食菜谱分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="美食分类" prop="className">
          <el-input v-model="form.className" placeholder="请输入美食分类" />
        </el-form-item>
        <el-form-item label="美食标题" prop="tile">
          <el-input v-model="form.tile" placeholder="请输入美食标题" />
        </el-form-item>
        <el-form-item label="地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="封面图" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">美食菜谱教程信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddGuideCookbookMethod">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteGuideCookbookMethod">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="guideCookbookMethodList" :row-class-name="rowGuideCookbookMethodIndex" @selection-change="handleGuideCookbookMethodSelectionChange" ref="guideCookbookMethod">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="美食标题" prop="tile" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tile" placeholder="请输入美食标题" />
            </template>
          </el-table-column>
          <el-table-column label="标题详情" prop="tileInfo" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.tileInfo" placeholder="请输入标题详情" />
            </template>
          </el-table-column>
          <el-table-column label="教程" prop="url" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.url" placeholder="请输入教程" />
            </template>
          </el-table-column>
          <el-table-column label="作者" prop="author" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.author" placeholder="请输入作者" />
            </template>
          </el-table-column>
          <el-table-column label="作者主页" prop="authorUrl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.authorUrl" placeholder="请输入作者主页" />
            </template>
          </el-table-column>

          <el-table-column label="做法类型" prop="type" width="150">
            <template slot-scope="scope">
              <el-select clearable v-model="scope.row.type">
                <el-option label="精选做法" value="精选做法"/>
                <el-option label="做法大全" value="做法大全"/>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 美食菜谱分类详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >
                        <el-descriptions-item label="美食分类" >{{form.className}}</el-descriptions-item>
                        <el-descriptions-item label="美食标题" >{{form.tile}}</el-descriptions-item>
                        <el-descriptions-item label="地址" >{{form.url}}</el-descriptions-item>
                        <el-descriptions-item label="封面图">
                          <image-preview :src="form.img" :width="50" :height="50"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="备注" >{{form.remark}}</el-descriptions-item>

      </el-descriptions>
          <el-divider content-position="center">美食菜谱教程信息</el-divider>
          <el-table :data="guideCookbookMethodList" :row-class-name="rowGuideCookbookMethodIndex" @selection-change="handleGuideCookbookMethodSelectionChange" ref="guideCookbookMethod">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
              <el-table-column label="美食标题" prop="tile" width="150" />
              <el-table-column label="标题详情" prop="tileInfo"  />
              <el-table-column label="菜品效果" prop="img">
                <template slot-scope="scope">
                  <a :href="scope.row.url">
                    <image-preview :src="scope.row.img" :width="50" :height="50"/>
                  </a>
                </template>
              </el-table-column>
              <el-table-column label="菜品效果" prop="url">
                <template slot-scope="scope">
                  <a :href="scope.row.url">{{scope.row.url}}</a>
                </template>
              </el-table-column>
              <el-table-column label="教程地址" prop="url" width="150" />
              <el-table-column label="作者" prop="author" width="150" />
              <el-table-column label="作者主页" prop="authorUrl">
                <template slot-scope="scope">
                  <a :href="scope.row.authorUrl">{{scope.row.authorUrl}}</a>
                </template>
              </el-table-column>
            <el-table-column label="做法类型" prop="type" width="150" >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.type  == '精选做法'" type="success">{{scope.row.type}}</el-tag>
                <el-tag v-if="scope.row.type  == '做法大全'" type="warning">{{scope.row.type}}</el-tag>
              </template>
            </el-table-column>
          </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCookbook, getCookbook, delCookbook, addCookbook, updateCookbook } from "@/api/guide/cookbook";


export default {
  name: "Cookbook",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedGuideCookbookMethod: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 美食菜谱分类表格数据
      cookbookList: [],
      // 美食菜谱教程表格数据
      guideCookbookMethodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        tile: null,
        url: null,
        img: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询美食菜谱分类列表 */
    getList() {
      this.loading = true;
      listCookbook(this.queryParams).then(response => {
        this.cookbookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        className: null,
        tile: null,
        url: null,
        img: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.guideCookbookMethodList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加美食菜谱分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCookbook(id).then(response => {
        this.form = response.data;
        this.guideCookbookMethodList = response.data.guideCookbookMethodList;
        this.open = true;
        this.title = "修改美食菜谱分类";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids
      getCookbook(id).then(response => {
        this.form = response.data;
            this.guideCookbookMethodList = response.data.guideCookbookMethodList;
        this.openInfo = true;
        this.title = "美食菜谱分类详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.guideCookbookMethodList = this.guideCookbookMethodList;
          if (this.form.id != null) {
            updateCookbook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCookbook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除美食菜谱分类编号为"' + ids + '"的数据项？').then(function() {
        return delCookbook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 美食菜谱教程序号 */
    rowGuideCookbookMethodIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 美食菜谱教程添加按钮操作 */
    handleAddGuideCookbookMethod() {
      let obj = {};
      obj.tile = "";
      obj.url = "";
      obj.img = "";
      obj.author = "";
      obj.authorUrl = "";
      obj.tileInfo = "";
      obj.classUrl = "";
      obj.type = "";
      obj.remark = "";
      this.guideCookbookMethodList.push(obj);
    },
    /** 美食菜谱教程删除按钮操作 */
    handleDeleteGuideCookbookMethod() {
      if (this.checkedGuideCookbookMethod.length == 0) {
        this.$modal.msgError("请先选择要删除的美食菜谱教程数据");
      } else {
        const guideCookbookMethodList = this.guideCookbookMethodList;
        const checkedGuideCookbookMethod = this.checkedGuideCookbookMethod;
        this.guideCookbookMethodList = guideCookbookMethodList.filter(function(item) {
          return checkedGuideCookbookMethod.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleGuideCookbookMethodSelectionChange(selection) {
      this.checkedGuideCookbookMethod = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('guide/cookbook/export', {
        ...this.queryParams
      }, `cookbook_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
